package com.lam.framework.security.handler;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import com.lam.common.base.domain.LoginUser;
import com.lam.common.base.domain.Result;
import com.lam.common.context.TokenUtils;
import com.lam.common.utils.DateUtil;
import com.lam.common.utils.IpUtils;
import com.lam.common.utils.JsonUtil;
import com.lam.common.utils.ServletUtils;
import com.lam.framework.operLog.enums.BusinessStatus;
import com.lam.framework.operLog.enums.BusinessType;
import com.lam.framework.operLog.enums.LogSave;
import com.lam.framework.operLog.enums.OperatorType;
import com.lam.framework.operLog.handlers.LogHandler;
import com.lam.framework.operLog.model.OperateLog;

/**
 * 自定义退出处理类 返回成功
 * 
 * @author lam
 */
@Configuration
public class LamLogoutSuccessHandler implements LogoutSuccessHandler {
	
	/**
	 * 退出处理
	 * 
	 * @return
	 */
	@Override
	public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)throws IOException, ServletException {
		
		long start = System.currentTimeMillis();
		Result result = Result.ok("退出成功");
		LoginUser loginUser = TokenUtils.getLoginUser(request);
		if (loginUser != null) {
			// 删除用户缓存记录
			TokenUtils.logout(loginUser.getToken());
			// 记录用户退出日志
			log(request, loginUser, result, start);
		}
		ServletUtils.outWrite(response, result);
	}
	
	private void log(HttpServletRequest request, LoginUser loginUser, Result result, long start) {
		OperateLog operLog = new OperateLog();
		operLog.setTitle("后端登录");//模块
		operLog.setOper("退出登录");//功能描述 
		operLog.setBusinessType(BusinessType.LOGIN_OUT.getType());// 设置业务操作类型 
		operLog.setOperatorType(OperatorType.ADMIN.getType());// 设置操作人类别
		operLog.setLogSave(LogSave.AUTO);
		operLog.setSaveReqData(1);
		operLog.setSaveResData(1);
		operLog.setPrintReqData(1);
		operLog.setPrintResData(1);
		operLog.setElapsedTime(1);
		operLog.setPrintIp(1);
		// 处理方法名称
		operLog.setHandler("com.lam.framework.security.handler.LamLogoutSuccessHandler.onLogoutSuccess()");
		operLog.setReqMethod("POST");// 设置请求方式
		operLog.setReqUrl(request.getRequestURI());// 请求的地址
		operLog.setReqIp(IpUtils.getIpAddr(request));// 请求的ip
		
		operLog.setReqParam("");
		operLog.setOperParam(loginUser.getToken());
		LogHandler.collectClientInfo(request, operLog);
		operLog.setResResult(JsonUtil.toJsonString(result, false, false));
		operLog.setReqStatus(BusinessStatus.SUCCESS.getCode());
		operLog.setReqDesc("退出成功");
		
		// 获取当前的用户
		operLog.setOperName(loginUser.getUsername());
		operLog.setOperTime(DateUtil.getCurTime());
		operLog.setRemark("");
		operLog.setElTime(System.currentTimeMillis() - start);
		LogHandler.submitLog(operLog);
	}
}
